﻿using System;
using System.Net;
using System.Text;
using System.Windows;
using eLearning.Model;
using eLearning.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;
using Coding4Fun.Toolkit.Controls;
using System.Windows.Input;

namespace eLearning.MahasiswaView
{
    public partial class MyKuliahPage : PhoneApplicationPage
    {
        private Perkuliahan kuliah;

        public MyKuliahPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(DataContextPage_Loaded);
        }

        private void DataContextPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (PhoneApplicationService.Current.State.ContainsKey(Config.KULIAH))
            {
                kuliah = (Perkuliahan)PhoneApplicationService.Current.State[Config.KULIAH];
            }
            if (kuliah != null)
            {
                textPerkuliahan.Text = kuliah.DetMatkul.Matkul.Nama;
                kuliah.Mhs = (Mahasiswa)SavedUser.Instance.User.ConvertUser();
            }
        }

        private void LeaveCurrentPerkuliahan()
        {
            string json = null, url = null;

            if (kuliah != null)
            {
                url = Config.Instance.PERKULIAHAN_SERVICE_URL() + Config.LEAVE_ACTION_KEY;
                json = JsonConvert.SerializeObject(kuliah);

                Uri uri = new Uri(url, UriKind.Absolute);
                WebClient client = new WebClient();

                client.Encoding = Encoding.UTF8;
                client.Headers[HttpRequestHeader.ContentType] = "application/json";
                client.Headers[HttpRequestHeader.ContentLength] = json.Length.ToString();

                client.UploadStringCompleted += new UploadStringCompletedEventHandler(leavePerkuliahan_UploadStringCompleted);

                try
                {
                    client.UploadStringAsync(uri, "POST", json);
                }
                catch (Exception) { }
            }
        }

        private void leavePerkuliahan_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Cancelled && e.Error != null)
                    return;

                MessageBox.Show("Message : " + e.Result);
                if (e.Result.Contains("Success"))
                    NavigationService.GoBack();

            }
            catch (Exception ex)
            {
                MessageBox.Show("ERROR : " + ex.Message);
            }
        }

        private void buttonNilai_Click(object sender, RoutedEventArgs e)
        {
            Utility.GotoPage(this, "/View/Mahasiswa/NilaiKuliahPage.xaml");

            PhoneApplicationService.Current.State[Config.KULIAH] = kuliah;
        }

        private void buttonMateri_Click(object sender, RoutedEventArgs e)
        {
            Utility.GotoPage(this, "/View/Mahasiswa/ResourcesPage.xaml");

            PhoneApplicationService.Current.State[Config.KULIAH] = kuliah;
        }

        private void buttonTugas_Click(object sender, RoutedEventArgs e)
        {
            Utility.GotoPage(this, "/View/Mahasiswa/TugasPage.xaml");

            PhoneApplicationService.Current.State[Config.KULIAH] = kuliah;
        }

        private void buttonQuiz_Click(object sender, RoutedEventArgs e)
        {
            Utility.GotoPage(this, "/View/Mahasiswa/QuizMhsPage.xaml");

            PhoneApplicationService.Current.State[Config.KULIAH] = kuliah;
        }

        private void buttonPengumuman_Click(object sender, RoutedEventArgs e)
        {
            Utility.GotoPage(this, "/View/PengumumanMatkulPage.xaml");
            PhoneApplicationService.Current.State[Config.DETMATKUL] = kuliah.DetMatkul;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void menuLeave_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Leave " + kuliah.DetMatkul.Matkul.Nama + "?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                InputPrompt prompt = new InputPrompt();
                prompt.Message = "Confirm password?";
                prompt.Completed += new EventHandler<PopUpEventArgs<string, PopUpResult>>(prompt_Completed);
                prompt.InputScope = new InputScope { Names = { new InputScopeName() { NameValue = InputScopeNameValue.Password } } };
                prompt.Show();

            }
        }

        private void prompt_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
        {
            if (!string.IsNullOrEmpty(e.Result))
            {
                if (e.Result.Equals(SavedUser.Instance.User.Password))
                    LeaveCurrentPerkuliahan();
                else
                    MessageBox.Show("Password tidak cocok!");
            }
        }

    }
}